﻿/********************************************************************************
** auth： Kadu
** date： 2018/01/24
** desc： 数据中心
** Ver.:  V1.0.0
*********************************************************************************/
using System.Collections.Generic;

#pragma warning disable 0649

namespace SimpleFramework
{
    public static partial class DataCenter
    {
        private static Dictionary<System.Type, IIdxTable> g_IdxDict = new Dictionary<System.Type, IIdxTable>();
        private static Dictionary<System.Type, IKeyTable> g_KeyDict = new Dictionary<System.Type, IKeyTable>();
        private static Dictionary<System.Type, JsonConfig> g_JsonDict = new Dictionary<System.Type, JsonConfig>();
        private static int g_LoadCount = 0;
        private static System.Action g_LoadFinish;

        public static void Add(System.Type type, IIdxTable table)
        {
            g_IdxDict[type] = table;
        }

        public static void Add(System.Type type, IKeyTable table)
        {
            g_KeyDict[type] = table;
        }

        public static void AddJson<T>()
        {
            g_JsonDict[typeof(T)] = null;
        }

        public static T GetJson<T>() where T: JsonConfig
        {
            JsonConfig config;
            if (g_JsonDict.TryGetValue(typeof(T), out config))
            {
                return (T)config;
            }
            return default(T);
        }

        public static void Load(System.Action<ILoader, byte[]> onProgress, System.Action onComplete)
        {
#if UNITY_EDITOR
            var stopwatch = new System.Diagnostics.Stopwatch();
            stopwatch.Start();
            g_LoadFinish = () =>
            {
                stopwatch.Stop();
                var ts = stopwatch.Elapsed;
                UnityEngine.Debug.LogFormat("加载配置耗时{0}ms", ts.TotalMilliseconds);
            };
#endif

            g_LoadCount = g_IdxDict.Count + g_KeyDict.Count;
            g_LoadFinish += onComplete;
            foreach (var pair in g_IdxDict)
            {
                var fileName = pair.Key.Name.Substring(3).ToLower();
                var assetPath = "config/" + fileName + ".bin";
                var url = PathSystem.Instance.Search(assetPath);
                LoadConfig(url, pair.Value, onProgress);
            }
            foreach (var pair in g_KeyDict)
            {
                var fileName = pair.Key.Name.Substring(3).ToLower();
                var assetPath = "config/" + fileName + ".bin";
                var url = PathSystem.Instance.Search(assetPath);
                LoadConfig(url, pair.Value, onProgress);
            }

            foreach (var pair in g_JsonDict)
            {
                var fileName = pair.Key.Name.Replace("Config", "").ToLower();
                var assetPath = "config/" + fileName + ".json";
                var url = PathSystem.Instance.Search(assetPath);
                LoadConfig(url, pair.Key, g_JsonDict, onProgress);
            }
        }

        private static void LoadConfig(string path, IIdxTable table, System.Action<ILoader, byte[]> onProgress)
        {
            FileSystem.Instance.LoadFile(path, (ILoader loader, byte[] bytes) =>
            {
                onProgress?.Invoke(loader, bytes);
                table.Read(bytes);
                g_LoadCount--;
                if (g_LoadCount <= 0)
                {
                    LoadFinish();
                }
            });
        }

        private static void LoadConfig(string path, IKeyTable table, System.Action<ILoader, byte[]> onProgress)
        {
            FileSystem.Instance.LoadFile(path, (ILoader loader, byte[] bytes) =>
            {
                onProgress?.Invoke(loader, bytes);
                table.Read(bytes);
                g_LoadCount--;
                if (g_LoadCount <= 0)
                {
                    LoadFinish();
                }
            });
        }

        private static void LoadConfig(string path, System.Type type, Dictionary<System.Type, JsonConfig> dict, System.Action<ILoader, byte[]> onProgress)
        {
            FileSystem.Instance.LoadFile(path, (ILoader loader, byte[] bytes) =>
            {
                onProgress?.Invoke(loader, bytes);
                var text = System.Text.Encoding.UTF8.GetString(bytes);
                var data = (JsonConfig)LitJson.JsonMapper.ToObject(text, type);
                dict[type] = data;
                g_LoadCount--;
                if (g_LoadCount <= 0)
                {
                    LoadFinish();
                }
            });
        }

        private static void LoadFinish()
        {
            g_LoadFinish?.Invoke();
            g_LoadFinish = null;
        }

        //public static T Get<T>(string key1) where T : IKeyEntity, new()
        //{
        //    IKeyTable result;
        //    if (g_KeyDict.TryGetValue(typeof(T), out result))
        //    {
        //        return (T)result.Get(key1);
        //    }
        //    return default(T);
        //}

        //public static T Get<T>(int key1) where T : IIdxEntity, new()
        //{
        //    IIdxTable result;
        //    if (g_IdxDict.TryGetValue(typeof(T), out result))
        //    {
        //        return (T)result.Get(key1);
        //    }
        //    return default(T);
        //}
    }
}